import { select } from '../../utils/arrays'
import { pad } from '../../utils/filters'

export function fixed (b) {
  var s = String(b)
  var idx = s.indexOf('.')
  if (idx > -1) {
    var [l, r] = s.split('.')
    var c = r.charAt(2)
    if (c == 5) {
      var num = l + '.' + r.charAt(0)
      var d = r.charAt(1)
      if (d % 2 == 0) { // 偶
        num += d
      } else {
        num += d + 1
      }
      return Number(num)
    }
  }
  return b.toFixed(2)
}

// 获取选中赛事玩法的最低/高赔率数组
export function minmaxOdds (map, bet) {
  return Object.keys(bet).reduce((o, k) => {
    var bets = bet[k]
    var m = map[k]
    o[k] = mappingOdds(m, bets)
    return o
  }, {})
}

function mappingOdds (m, bets) {
  var min = 0, max = 0
  bets.forEach(bet => {
    var [rule, b] = bet.split('|')
    var d = mapping(m, rule)
    var odds = betToOdds(rule, b, d)
    if (min == 0) {
      min = max = odds
    } else {
      min = Math.min(min, odds)
      max = Math.max(max, odds)
    }
  })
  return [min, max]
}

function betToOdds (rule, bet, data) {
  var key
  switch (rule) {
    case 'rqspf':
    case 'spf':
      bet = bet.replace('让球', '')
      if (bet == '胜') {
        key = 'win'
      } else if (bet == '负') {
        key = 'lose'
      } else {
        key = 'flat'
      }
      break
    case 'bqc':
      key = bet.replace(/胜/g, 'h').replace(/平/g, 'd').replace(/负/g, 'a')
      break
    case 'zjq':
      key = 's' + bet
      break
    case 'bf':
      switch (bet) {
        case '胜其它':
          key = '-1-h'
          break
        case '平其它':
          key = '-1-d'
          break
        case '负其它':
          key = '-1-a'
          break
        default:
          var [h, g] = bet.split(':')
          key = pad(h) + pad(g)
      }
  }
  return data[key]
}

export function isBetOne (m, rule) {
  var d = mapping(m, rule)
  return d && d.isBetOne
}

// 获取完整指定玩法的赔率数据
export function mapping (m, rule) {
  switch (rule) {
    case 'rqspf':
      return m['hhad']
    case 'bqc':
      return m['hafu']
    case 'spf':
      return m['had']
    case 'zjq':
      return m['ttg']
    case 'bf':
      return m['crs']
    case 'hhgg':
      return true
  }
}

export const res = {3: '胜', 1: '平', 0: '负'}

// 构造投注内容
export function betContent (bet, rule) {
  if (rule == 'hhgg') {
    return Object.keys(bet).map(k => {
      // 按玩法分组
      var group = grouping(bet[k])
      return Object.keys(group).map(g => {
        // 玩法拼接投注数组
        return g + '|' + group[g].join(',')
        // 赛事ID拼接玩法/投注数组
      }).map(v => k + '|' + v).join(';')
    }).join(';')
  } else {
    return Object.keys(bet).map(k => {
      var val = bet[k].map(v => v.split('|')[1])
      return k + '|' + val
    }).join(';')
  }
}

// 按投注玩法分组
function grouping (val) {
  return val.reduce((o, curr) => {
    var bets = curr.split('|')
    var rule = bets[0]
    var arr = o[rule]
    if (!arr) {
      arr = []
      o[rule] = arr
    }
    arr.push(bets[1])
    return o
  }, {})
}

export function color (n) {
  return n == 0 ? 'grey' : n > 0 ? 'orange' : 'green'
}

export const mixin = {
  props: ['m', 'value'],
  data () {
    return {
      selected: this.value || []
    }
  },
  methods: {
    select (v) {
      this.$emit('input', select(this.value, v))
    },
    color,
    on (val) {
      return this.bets.includes(val) ? 'on' : ''
    }
  },
  computed: {
    bets () {
      return this.value || []
    }
  }
}
